package com.guigu.dealservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.R;
import com.guigu.dealservice.dto.EquipmentType;
import com.guigu.dealservice.entity.PurchaseDemand;
import com.guigu.dealservice.feign.MyEquipmentFeign;
import com.guigu.dealservice.feign.MyUserFeign;
import com.guigu.dealservice.service.PurchaseDemandService;
import com.guigu.dealservice.vo.PurchaseDemandVo;

import com.guigu.equipmentservice.service.EquipmentTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 求购信息表（存储买家发布的二手机械求购需求） 前端控制器
 * </p>
 *
 * @author ysp
 * @since 2025-10-10
 */
@RestController
@RequestMapping("/dealservice/purchaseDemand")
public class PurchaseDemandController {

    @Autowired
    private PurchaseDemandService purchaseDemandService;

    @Autowired
    private MyEquipmentFeign myEquipmentFeign;


    /**
     * 前台-查询前8最新求购信息
     * */
    @GetMapping("/selectPurchaseDemandByTime")
    public R selectPurchaseDemandByTime() {

        QueryWrapper<PurchaseDemand> purchaseDemandQueryWrapper = new QueryWrapper<>();
        purchaseDemandQueryWrapper.eq("purchase_status",0);
        purchaseDemandQueryWrapper.orderByDesc("release_time");
        purchaseDemandQueryWrapper.last("LIMIT 8");

        List<PurchaseDemand> purchaseDemandList = purchaseDemandService.list(purchaseDemandQueryWrapper);

        for (int i=0;i<purchaseDemandList.size();i++){
            PurchaseDemand purchaseDemand = purchaseDemandList.get(i);

            //远程调用拿到设备类型名称
            EquipmentType equipmentType = myEquipmentFeign.queryEquipmentTypeById(purchaseDemand.getTypeId());
            purchaseDemand.setTypeName(equipmentType.getTypeName());
        }

        return R.ok().data("purchaseDemandPageInfo",purchaseDemandList);
    }


    @PostMapping("/selectPurchaseDemandByPage/{pageNum}/{pageSize}")
    public R selectPurchaseDemandByPage(@PathVariable("pageNum") int pageNum,
                                        @PathVariable("pageSize") int pageSize,
                                        @RequestBody PurchaseDemandVo purchaseDemandVo) {
        return purchaseDemandService.queryPurchaseDemandByPage(pageNum,pageSize,purchaseDemandVo);
    }

    @PutMapping("/allocatePurchaseDemand/{id}/{businessmanId}")
    public R allocatePurchaseDemand(@PathVariable("id") Integer id,
                                    @PathVariable("businessmanId") Long businessmanId) {
        return purchaseDemandService.allocatePurchaseDemand(id,businessmanId);
    }

    /**
     * 根据业务员ID查新求购表
     * @param bid
     * @return
     */
    @GetMapping("/selectPurchaseByEId/{bid}")
    public R selectPurchaseByEId(@PathVariable("bid") Integer bid){
        return purchaseDemandService.selectPurchaseByEId(bid);
    }

    //根据求购ID修改求购状态
    @PutMapping("/updatePurchaseStatus/{id}")
    public Boolean updatePurchaseStatus(@PathVariable("id") Integer id){
        PurchaseDemand purchaseDemand = purchaseDemandService.getById(id);
        purchaseDemand.setPurchaseStatus(3);
        return purchaseDemandService.updateById(purchaseDemand);
    }

    /**
     * 前台-添加求购信息
     *
     */

    @PostMapping("/addPurchaseDemand")
    public R addPurchaseDemand(@RequestBody PurchaseDemand purchaseDemand) {
        purchaseDemand.setPurchaseStatus(0);
        return purchaseDemandService.addPurchaseDemand(purchaseDemand);
    }

    //根据求购ID查询求购信息
    @GetMapping("/selectPurchaseById/{id}")
    public R selectPurchaseById(@PathVariable("id") Integer id){
        return purchaseDemandService.selectPurchaseById(id);
    }


    /**
     * 前台-根据用户id查询我的求购哦列表
     */
    @PostMapping("/queryPurchaseDemandByUserId/{pageNum}/{pageSize}")
    public R queryPurchaseDemandByUserId(@PathVariable("pageNum") Integer pageNum,
                                         @PathVariable("pageSize") Integer pageSize,
                                        @RequestBody PurchaseDemandVo vo) {
        R r = purchaseDemandService.queryPurchaseDemandByUserId(pageNum, pageSize, vo);
        return r;
    }

    //根据用户ID查询求购
    @GetMapping("/selectPurchaseByUserId/{id}")
    public R selectPurchaseByUserId(@PathVariable("id") Integer id){
        QueryWrapper<PurchaseDemand> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        List<PurchaseDemand> list = purchaseDemandService.list(wrapper);
        for (PurchaseDemand pd: list
             ) {
            //根据设备类型ID查询类型表
            EquipmentType equipmentType = myEquipmentFeign.queryEquipmentTypeById(pd.getTypeId());
            pd.setTypeName(equipmentType.getTypeName());
        }
        return R.ok().data("list",list);
    }

    /**
     * 前台-根据用户id查询分页查询求购信息
     * @param pageNum
     * @param pageSize
     * @param purchaseDemandVo
     * @return
     */
    @PostMapping("/selectPurchaseByUserIdPC/{pageNum}/{pageSize}/{userId}")
    public R selectPurchaseByUserIdPC(@PathVariable("pageNum") Integer pageNum,
                                        @PathVariable("pageSize") Integer pageSize,
                                        @PathVariable("userId") Long userId,
                                        @RequestBody PurchaseDemandVo purchaseDemandVo) {
        PageInfo<PurchaseDemand> purchaseDemandPageInfo = purchaseDemandService.queryPurchaseDemandByPageUserIdPC(pageNum, pageSize, userId, purchaseDemandVo);
        return R.ok().data("list",purchaseDemandPageInfo);
    }
    /**
     * 前台-根据求购id删除求购信息
     * @param id
     * @return
     */
    @DeleteMapping("/deletePurchaseDemand/{id}")
    public R deletePurchaseDemand(@PathVariable("id") Integer id){
        boolean b = purchaseDemandService.removeById(id);
        if (b==true)
            return R.ok().code(20000).message("删除成功").data("success", true);
        return R.error().code(50000).message("删除失败").data("success", false);
    }
    /**
     * 前台-根据求购查询求购信息
     * @param id
     * @return
     */
    @GetMapping("/selectPurchaseByIdPC/{id}")
    public R selectPurchaseByIdPC(@PathVariable("id") Integer id){
        PurchaseDemand byId = purchaseDemandService.getById(id);
        return R.ok().data("purchaseDemand",byId);
    }
    /**
     * 前台-修改求购信息
     * @param
     * @return
     */
    @PutMapping("/updatePurchaseById")
    public R updatePurchaseById(@RequestBody PurchaseDemand purchaseDemand){
        boolean b = purchaseDemandService.updateById(purchaseDemand);
        if (b==true)
            return R.ok().code(20000).message("修改成功").data("success", true);
        return R.error().code(50000).message("修改失败").data("success", false);
    }

    /**
    * 前台-添加求购信息
     * @return
     */
    @PostMapping("/addQuiGouPurchaseDemand")
    public R addQuiGouPurchaseDemand(@RequestBody PurchaseDemand purchaseDemand) {
        purchaseDemand.setPurchaseStatus(0);
        purchaseDemand.setReleaseTime(LocalDateTime.now());
        boolean b = purchaseDemandService.save(purchaseDemand);
        if (b)
         return R.ok().code(20000).message("添加成功").data("success", true);
        return R.error().code(20001).message("添加失败").data("success", false);
    }


    /**
     * 前台-根据用户id和求购id删除求购
     */
    @DeleteMapping("/deletePurchaseDemandByBidAndPid/{uid}/{pid}")
    public R deletePurchaseDemandByBidAndPid(@PathVariable("uid") Integer uid,
                                             @PathVariable("pid") Integer pid)
    {
        return purchaseDemandService.deletePurchaseDemandByBidAndPid(uid,pid);
    }


    /**
     * 前台-根据用户id和求购id修改求购
     */
    @PutMapping("/updatePurchaseDemandByBidAndPid")
    public R updatePurchaseDemandByBidAndPid(@RequestBody PurchaseDemand purchaseDemand)
        {
        return purchaseDemandService.updatePurchaseDemandByBidAndPid(purchaseDemand);
    }

    //查询所有求购信息
    @GetMapping("/selectAllPurchaseDemand")
    public R selectAllPurchaseDemand() {
        List<PurchaseDemand> list = purchaseDemandService.list();
        return R.ok().data("list",list);
    }
}
